package cn.edu.cxcyzx.controller;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;











import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.cxcyzx.conf.Role;
import cn.edu.cxcyzx.entity.ApplyProject;
import cn.edu.cxcyzx.entity.CheckProject;
import cn.edu.cxcyzx.entity.MemberPOJO;
import cn.edu.cxcyzx.entity.Members;
import cn.edu.cxcyzx.entity.MiddleCheck;
import cn.edu.cxcyzx.entity.ProjectInfo;
import cn.edu.cxcyzx.entity.Student;
import cn.edu.cxcyzx.entity.Task;
import cn.edu.cxcyzx.entity.TaskPOJO;
import cn.edu.cxcyzx.entity.Teacher;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.supervisor.ServiceFit;
import cn.edu.cxcyzx.util.MsgJSON;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Resource
	ServiceFit serviceFit;
	Role r=Role.TEA;
	private Teacher tea=null;
	@RequestMapping("home")
	public ModelAndView HomePage(HttpSession session){
		this.tea=(Teacher) session.getAttribute("teacher");
		if(tea==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		System.out.println("首页");
		return new ModelAndView("teacher/teaIndex");
	}
	/**
	 * 项目管理
	 * 		项目信息
	 * @return
	 */
	@RequestMapping("project")
	public ModelAndView projectPage(Model model){
		if(tea==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		//项目信息
		ApplyProject applyProject=serviceFit.getApplyProjectService().getApplyBy_leaderId(tea.getTeaId());
		if(applyProject==null){
			System.out.println("未申报");
			//未申报
			model.addAttribute("applyProject", null);
			model.addAttribute("projectInfo", null);
			return new ModelAndView("teacher/project");
		}else{
			System.out.println("已申报");
			//已申报
			int status=applyProject.getProjectStatus();
			System.out.println("status:"+status);
			if(status==0||status==2){//申报状态：待定/驳回
				//申报时期
				model.addAttribute("applyProject",applyProject);
				model.addAttribute("projectInfo", null);
			}else{
				System.out.println("申报通过");
				//申报通过
				ResultDo<ProjectInfo> resultDo=serviceFit.getProjectService().getProjectInfo(applyProject.getProjectId());
				ProjectInfo projectInfo=resultDo.getResult();
				System.out.println("status:"+projectInfo.getProject().getProjectStatus());
				if(projectInfo.getProject().getProjectStatus()==10||projectInfo.getProject().getProjectStatus()==0){
					System.out.println("提交验收申请");
					//提交验收申请
					CheckProject checkProject=serviceFit.getCheckProjectService().getCheckProject(projectInfo.getProject().getProjectId());
					System.out.println("checkProject:"+checkProject);
					if(checkProject!=null){
						model.addAttribute("checkProject", checkProject);
					}
				}
				if(projectInfo.getProject().getProjectStatus()>=2){
					System.out.println("需求已经提交");
					//需求已经提交
					ResultDo<Map<Task,List<Student>>> resultDo2=serviceFit.getTaskService().getProTask(projectInfo.getProject().getProjectId());
					System.out.println("resultDo2:"+resultDo2.isSuccess());
					if(resultDo2.isSuccess()){
						Map<Task,List<Student>> tasks=resultDo2.getResult();
						
						int tasksStatus=0;//完成了多少个任务
						Set<Task> set=tasks.keySet();
						for (Task task2 : set) {
							if(task2.getTaskStatus()==3){
								tasksStatus++;
							}
						}
						model.addAttribute("tasksStatus", tasksStatus);
						model.addAttribute("tasks", tasks);
						System.out.println("tasksStatus:"+tasksStatus);
						System.out.println("tasks"+tasks.size());
					}
				}
				
				if(resultDo.isSuccess()){
					List<Members> members=projectInfo.getMembers();
					int can_applyTask=0;//不可以分配
					
					for (Members members2 : members) {
						if(members2.getTaskId()==null){
							//有未分配任务的学生
							can_applyTask=1;
						}
					}
					model.addAttribute("can_applyTask", can_applyTask);
					model.addAttribute("applyProject", null);
					model.addAttribute("projectInfo", projectInfo);
				}
				//中期检查
				ResultDo<MiddleCheck> resultDo2=serviceFit.getMiddleCheckService().getMiddleCheck(applyProject.getProjectId());
				if(resultDo2.isSuccess()){
					model.addAttribute("middleCheck", resultDo2.getResult());
				}
			}
		}
		
		return new ModelAndView("teacher/project");
		
		//申报项目
		
		//提交需求分析
		

	}
	
	/**
	 * 申报项目
	 * @param applyProject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="creatproject",method=RequestMethod.POST)
	
	public ModelAndView projectPage(ApplyProject applyProject,HttpSession session,Model model){
		
		int projectId=201800+serviceFit.getApplyProjectService().getId()+1;
		applyProject.setProjectId(projectId);
		applyProject.setLeaderId(tea.getTeaId());
		applyProject.setLeaderName(tea.getTeaName());
		applyProject.setLeaderRole(1);
		serviceFit.getApplyProjectService().insertApply(applyProject);
		System.out.println(applyProject.toString());
		model.addAttribute("msg", "申报成功");
		model.addAttribute("project", serviceFit.getApplyProjectService().getApplyBy_leaderId(tea.getTeaId()));
		int applyNum=serviceFit.getApplyProjectService().getApplyNum_0();//申报请求
		session.setAttribute("applyNum", applyNum);
		return new ModelAndView("teacher/project");
	}
	/**
	 * 判断gitee_proName是否存在
	 * @param gitee_proName
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/haveGiteeProName")
	@ResponseBody
	public MsgJSON haveGiteeProName(String gitee_proName,HttpSession session){
		String userId=(String) session.getAttribute("roleID");
		if(gitee_proName==null||gitee_proName.trim().equals("")){
			return MsgJSON.fail().add("msg", "请正确输入Gitee项目名");
		}else{
			ResultDo<String> resultDo=serviceFit.getGiteeService().have_proName(gitee_proName, userId);
			if(resultDo.isSuccess()){
				return MsgJSON.success().add("full_name", resultDo.getResult());
			}else{
				return MsgJSON.fail().add("msg", resultDo.getMessage());
			}
		}
	}
	/**
	 * 任务分配
	 * @param model
	 * @param taskId
	 * @param stuId
	 * @param stuName
	 * @return
	 */
	@RequestMapping(value="distributeTask")
	@ResponseBody
	public MsgJSON distributeTask(Model model,String taskId,String[] stuId){
		System.out.println("taskId:"+taskId+",stuId:"+Arrays.toString(stuId));
		serviceFit.getTaskMembersService().inserMembers(taskId, stuId);
		return MsgJSON.success();
		
	}
	
	/**
	 * 新增任务
	 * @param task
	 * @return
	 */
	@RequestMapping("/saveTask")
	@ResponseBody
	public MsgJSON saveTask(String[] members,String branch,String projectId,String taskName,String task_introduction,String taskFile){
		Task task=new Task(Integer.parseInt(projectId),branch,serviceFit.getTaskService().get_taskId(), taskName, task_introduction, taskFile);
		System.out.println(Arrays.toString(members));
		System.out.println(task.toString());
		
		serviceFit.getTaskService().insertOneTask(task,members);
	
		return MsgJSON.success();
		
		
		

	}
	
	/**
	 * 修改完成时间
	 * @param update_finishTime
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/updateFinishTime")
	@ResponseBody
	public MsgJSON updateFinishTime(java.sql.Date update_finishTime, int projectId){
		System.out.println("update_finishTime:"+update_finishTime+",projectId:"+projectId);
		serviceFit.getProjectService().update_finishTime(update_finishTime, projectId);
		return MsgJSON.success();
	}
	/**
	 * 申请验收
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/applyCheck")
	@ResponseBody
	public MsgJSON applyCheck(int projectId){
		
		System.out.println(",projectId:"+projectId);
		CheckProject checkProject=new CheckProject(projectId, 0);
		serviceFit.getCheckProjectService().insert_checkProject(checkProject);
		return MsgJSON.success().add("applyCheck_Status", 0);
	}
	/**
	 * 成员信息页面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("memberInfo")
	public ModelAndView memberPage(HttpSession session,Model model){
		System.out.println("memberInfo----");
		if(this.tea==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		return new ModelAndView("teacher/memberInfo");
	}
	/**
	 * 成员信息
	 * @param model
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("/getMemberInfo")
	@ResponseBody
	public MsgJSON getMemberInfo(Model model,@RequestParam(defaultValue="1")Integer pageNum){
		
		PageHelper.startPage(pageNum, 5);
		ResultDo<List<MemberPOJO>> resultDo=serviceFit.getProjectService().getMembersInfo(this.tea.getTeaId());
		if(resultDo.isSuccess()){
			System.out.println("---------ok");
			PageInfo<MemberPOJO> pageInfo=new PageInfo<>(resultDo.getResult(), 5);
			return MsgJSON.success().add("page", pageInfo);
		}else{
			System.out.println("---------fail");
			return MsgJSON.fail();
		}
		
	}
	/**
	 * 删除成员
	 * @param groupId
	 * @param stuId
	 * @return
	 */
	@RequestMapping("/deleteMember")
	public String deleteMember(String groupId,String stuId){
		System.out.println(groupId+","+stuId);
		serviceFit.getMembersService().delete_oneMember(groupId, stuId);
		return "memberInfo.do";	
	}
	/**
	 * 任务分配页面
	 * @return
	 */
	@RequestMapping("taskInfo")
	public ModelAndView taskInfoPage(){
		System.out.println("taskInfo----");
		if(this.tea==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		return new ModelAndView("teacher/taskInfo");
	}
	/**
	 * 获取任务信息
	 * @param model
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("/getTaskInfo")
	@ResponseBody
	public MsgJSON getTaskInfo(Model model,@RequestParam(defaultValue="1")Integer pageNum){
		
		PageHelper.startPage(pageNum, 5);
		ResultDo<List<TaskPOJO>> resultDo=serviceFit.getTaskService().getAllTaskBy_leaderId(this.tea.getTeaId());
		if(resultDo.isSuccess()){
			System.out.println("---------ok");
			PageInfo<TaskPOJO> pageInfo=new PageInfo<>(resultDo.getResult(), 5);
			return MsgJSON.success().add("page", pageInfo);
		}else{
			System.out.println("---------fail");
			return MsgJSON.fail();
		}
		
	}
	/**
	 * 删除任务
	 * @param taskId
	 * @return
	 */
	@RequestMapping("/deleteTask")
	public String deleteTask(String taskId){
		serviceFit.getTaskService().deleteTask(taskId);
		return "teacher/taskInfo";
		
	}
	/**
	 * 个人中心
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("teaInfo")
	public ModelAndView stuInfoPage(HttpSession session,Model model){
		if(this.tea==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		return new ModelAndView("teacher/teaInfo");
	}
	/**
	 * 重新登陆
	 * @param session
	 * @return
	 */
	@RequestMapping("/relogin")
	@ResponseBody
	public MsgJSON reLogin(HttpSession session){
		session.removeAttribute("teacher");
		return MsgJSON.success();
	}
	
	
	/**
	 * 修改个人信息
	 * @param Teacher
	 * @return
	 */
	@RequestMapping("upteaInfo")
	public ModelAndView upteaInfo(Teacher teacher){
		System.out.println("..updateStu:"+teacher.toString());
		serviceFit.getTeacherService().updateTea(teacher);
		return new ModelAndView("teacher/teaInfo");
	}
	@RequestMapping("quit")
	public String quit(HttpSession session){
		session.removeAttribute("teacher");
		return "redirect:/login.jsp";
	}
}
